package hzau.service.Impl.SecurityService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hzau.dao.UserDao;
import hzau.pojo.LoginUser;
import hzau.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
public  class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UserDao userDao;

    @Override

    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName,s);
        User user = userDao.selectOne(lqw);
        //如果没有查询到用户就抛出异常
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或者密码错误");
        }
        //TODO 查询对应权限信息

        //把数据封装成UserDetails
        return new LoginUser(user);
    }
}